package mi.payment.ui.admin;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import mi.payment.BuildConfig;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.db.PayDetailModel;
import mi.payment.db.SettlementModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.TextHintDialog;
import sunmi.sunmiui.title.Title;

/**
 * 清除交易流水
 * Created by lj on 2017/2/15.
 */
public class ClearTradingActivity extends ParentActivity {
    private View clearReversalWaterView, clearTradingWaterView, clearScriptDataView, clearBlacklistDataView;
    private TextHintDialog mTextHintDialog;
    private PayDetailModel mPayDetailModel;
    private SettlementModel mSettlementModel;
    private List<PayDetail> mList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remove_trading_water);
        mPayDetailModel = PayDetailModel.getInstance();
        mSettlementModel = SettlementModel.getInstance();
        initView();
        setClickListener();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        Title mTitle;
        TextView mLeftText, mRightText;
        mTitle = (Title) findViewById(R.id.clear_trading_water_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.remove_trading_water));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClearTradingActivity.this.finish();
            }
        }, null, null, null);

        clearReversalWaterView = findViewById(R.id.clear_reversal_water);
        clearTradingWaterView = findViewById(R.id.clear_trading_water);
        clearScriptDataView = findViewById(R.id.clear_script_data);
        clearBlacklistDataView = findViewById(R.id.clear_blacklist_data);

        mLeftText = (TextView) clearReversalWaterView.findViewById(R.id.item_left_text);
        mLeftText.setText(getResources().getString(R.string.clear_reversal_water));

        mLeftText = (TextView) clearTradingWaterView.findViewById(R.id.item_left_text);
        mLeftText.setText(getResources().getString(R.string.remove_trading_water));

        mLeftText = (TextView) clearScriptDataView.findViewById(R.id.item_left_text);
        mLeftText.setText(getResources().getString(R.string.clear_script_data));

        mLeftText = (TextView) clearBlacklistDataView.findViewById(R.id.item_left_text);
        mLeftText.setText(getResources().getString(R.string.clear_blacklist_data));

        mRightText = (TextView) clearReversalWaterView.findViewById(R.id.item_right_text);
        mRightText.setVisibility(View.GONE);

        mRightText = (TextView) clearTradingWaterView.findViewById(R.id.item_right_text);
        mRightText.setVisibility(View.GONE);

        mRightText = (TextView) clearScriptDataView.findViewById(R.id.item_right_text);
        mRightText.setVisibility(View.GONE);

        mRightText = (TextView) clearBlacklistDataView.findViewById(R.id.item_right_text);
        mRightText.setVisibility(View.GONE);
        if (Configs.getInstance().HIDE_BLACKLIST_DATA()) {//隐藏黑名单数据
            clearBlacklistDataView.setVisibility(View.GONE);
        }
        if (Configs.getInstance().HIDE_CLEAR_SCRIPT_DATA()) {//隐藏清除脚本数据
            clearScriptDataView.setVisibility(View.GONE);
        }
    }

    /**
     * 设置点击事件监听
     */
    private void setClickListener() {
        clearReversalWaterView.setOnClickListener(this);
        clearTradingWaterView.setOnClickListener(this);
        clearScriptDataView.setOnClickListener(this);
        clearBlacklistDataView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.clear_reversal_water:
                //清除冲正流水
                mList = mPayDetailModel.getWaitForCorrection();
                if (mList.size() <= 0) {
                    showToast(getString(R.string.no_reversal_water));
                } else {
                    mTextHintDialog = DialogCreater.createTextHintDialog(this, getResources().getString(R.string.clear_reversal_water), getResources().getString(R.string.cancel), getResources().getString(R.string.remove), getResources().getString(R.string.sure_to_correction), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mTextHintDialog.dismiss();
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                for (PayDetail paydetail : mList) {
                                    paydetail.isNeedReversal = false;
                                    paydetail.isReversal = true;
                                    mPayDetailModel.update(paydetail);
                                }
                                showToast(getString(R.string.clean_reversal_success));
                            } catch (Exception e) {
                                e.printStackTrace();
                                showToast(getString(R.string.clean_reversal_error));
                            } finally {
                                mTextHintDialog.dismiss();
                            }
                        }
                    }, false);
                    mTextHintDialog.show();
                }
                break;
            case R.id.clear_trading_water:
                //清除交易流水
                if (mPayDetailModel.getPayDetailCount() <= 0) {
                    showToast(getString(R.string.no_trading_water));
                } else {
                    mTextHintDialog = DialogCreater.createTextHintDialog(this, getResources().getString(R.string.remove_trading_water), getResources().getString(R.string.cancel), getResources().getString(R.string.remove), getResources().getString(R.string.sure_to_remove_trading_water), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mTextHintDialog.dismiss();
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                mPayDetailModel.deleteAll();
                                mSettlementModel.emptyTableData();
                                //插入结算表以免交易时结算空指针
                                String batchNumber = SystemParamsOperation.getInstance().getSystemParamsSettings().getBatchNumStart();
                                SettlementModel.getInstance().insertByBatchNo(batchNumber);
                                showToast(getString(R.string.clean_water_success));
                            } catch (Exception e) {
                                e.printStackTrace();
                                showToast(getString(R.string.clean_water_error));
                            } finally {
                                mTextHintDialog.dismiss();
                            }
                        }
                    }, false);
                    mTextHintDialog.show();
                }
                break;
            case R.id.clear_script_data:
                //清除脚本数据
                break;
            case R.id.clear_blacklist_data:
                //清除黑名单数据
                break;
        }
    }
}
